﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

using Carmall.Entity;
using Carmall.BLL;

namespace Carmall.Web.Controllers
{
    [Authorize]
    public class ProductController : Controller
    {

        #region 产品
        //
        // GET: /Product/

        public ActionResult Index()
        {
            List<ProductsInfo> list = BLLFactory<Products>.Instance.GetAll();
            return View(list);
        }

        public ActionResult Create()
        {
            return View();
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection form)
        {
            HttpFileCollectionBase f = HttpContext.Request.Files;
            var p = new ProductsInfo();

            TryUpdateModel(p, new string[] { 
                "Category",
                "Name",
                "Price",
                "Specification",
                "Notes",
                "ModelType",
                "Quantity"
            }, form.ToValueProvider());

            if (String.IsNullOrEmpty(p.Name))
            {
                ViewData.ModelState.AddModelError("ProductName", "请填写产品名字");
            }
            if (!(p.Price > 0))
            {
                ViewData.ModelState.AddModelError("ProductPrice", "请填写产品价格");
            }

            if (ModelState.IsValid)
            {
                PartbrandInfo pb = new PartbrandInfo();
                pb.BrandName = "";
                int pbid = BLLFactory<Partbrand>.Instance.Insert2(pb);
                p.Brand = pbid;
                int pid = BLLFactory<Products>.Instance.Insert2(p);
                //product carmodel relationship loop
                ProductCarModelRelInfo pcr = new ProductCarModelRelInfo();
                pcr.ProductID = pid;
                pcr.CarModelID = 1;
                BLLFactory<ProductCarModelRel>.Instance.Insert(pcr);

                //product color relationship loop
                ProductColorRelInfo pcolor = new ProductColorRelInfo();
                pcolor.ProductID = pid;
                pcolor.ColorID = 1;
                BLLFactory<ProductColorRel>.Instance.Insert(pcolor);

                //ProductPrice
                ProductPriceInfo pp = new ProductPriceInfo();
                pp.Created = DateTime.Now;
                pp.ProductID = pid;
                pp.ProductPrice = p.Price;
                int ppid = BLLFactory<ProductPrice>.Instance.Insert2(pp);

                //ProductFavorablePrice loop
                DateTime d = DateTime.Now;
                ProductFavorablePriceInfo pfp = new ProductFavorablePriceInfo();
                pfp.Created = d;
                pfp.MinNumber = 1;
                pfp.MaxNumber = 10;
                pfp.ProductID = pid;
                pfp.ProductPrice = p.Price;
                pfp.ProductPriceID = ppid;
                BLLFactory<ProductFavorablePrice>.Instance.Insert(pfp);

                //upload image
                HttpFileCollectionBase files = HttpContext.Request.Files;
                for (int i = 0; i < files.Count; i++)
                { 
                    HttpPostedFileBase postedFile = files[i];
                    string fileName = System.IO.Path.GetFileName(postedFile.FileName);
                    if (fileName != null && fileName != string.Empty)
                    {
                        string fName = DateTime.Now.Ticks.ToString() + fileName;
                        postedFile.SaveAs(Server.MapPath("~/upload/" + fName));
                        var file = new AttachmentInfo();
                        file.FileName = fName;
                        file.FileType = postedFile.ContentType;
                        file.ObjectType = 1;
                        file.Path = "/upload/";
                        file.ObjectID = pid;
                        file.Title = "";
                        BLLFactory<Attachment>.Instance.Insert(file);
                    }
                }
                return RedirectToAction("NotShelf");

            }

            return View();
            //return RedirectToAction("Index", "Manage");
            
        }


        public ActionResult NotShelf()
        {
            List<ProductsInfo> list = null;
            string condition = "State=0";
            list = BLLFactory<Products>.Instance.Find(condition);
            return View(list);
        }

        public ActionResult OnShelf()
        {
            List<ProductsInfo> list = null;
            string condition = "State=1";
            list = BLLFactory<Products>.Instance.Find(condition);
            return View(list);
        }

        public ActionResult Edit(int id)
        {
            return View();
        }

        public ActionResult Delete(string ids)
        {
            if (ids.Length > 0)
            {
                string[] idlist = ids.Split(',');
                foreach (string id in idlist)
                {
                    BLLFactory<Products>.Instance.Delete(id);
                    BLLFactory<ProductColorRel>.Instance.DeleteByCondition("ProductID=" + id);
                    BLLFactory<ProductCarModelRel>.Instance.DeleteByCondition("ProductID=" + id);
                    BLLFactory<ProductFavorablePrice>.Instance.Delete("ProductID=" + id);
                    BLLFactory<ProductPrice>.Instance.Delete("ProductID=" + id);
                    BLLFactory<Attachment>.Instance.Delete("ObjectID=" + id);
                }
            }
            return RedirectToAction("NotShelf", "Product");
        }

        public ActionResult Shangjia(string ids)
        {
            if (ids.Length > 0)
            {
                string[] idlist = ids.Split(',');
                foreach (string id in idlist)
                {
                    ProductsInfo p = BLLFactory<Products>.Instance.FindByID(id);
                    if (p != null)
                    {
                        p.Display = 1;
                        BLLFactory<Products>.Instance.Update(p, id);
                    }                    
                }
            }
            return RedirectToAction("OnShelf", "Product");
        }
        #endregion

        #region 类别

        public ActionResult Category()
        {
            List<CategoryInfo> list = BLLFactory<Category>.Instance.GetAll();
            return View(list);
        }

        public ActionResult AddCat()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddCat(FormCollection form)
        {
            var cat = new CategoryInfo();
            UpdateModel(cat, new string[] { "Category", "ParentID", "CategoryName" }, form.ToValueProvider());
            if (String.IsNullOrEmpty(cat.Category))
                ModelState.AddModelError("", "请填写类别名称");

            if (ModelState.IsValid)
            {
                BLLFactory<Category>.Instance.Insert(cat);
                return RedirectToAction("Success", "Home");
            }
            return View();
        }

        #endregion

        #region 车型
        public ActionResult CarType()
        {
            List<CarTypeInfo> list = BLLFactory<CarType>.Instance.GetAll();
            return View(list);
        }

        public ActionResult AddCarType()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddCarType(FormCollection form)
        {
            var ct = new CarTypeInfo();
            UpdateModel(ct, new string[] { 
                "Name", "BrandID", "Brand","Displacement","Size","CarModel" 
            }, form.ToValueProvider());

            if (String.IsNullOrEmpty(ct.Name)) {
                ModelState.AddModelError("", "请填写类别名！");
            }
            if (ModelState.IsValid)
            {
                BLLFactory<CarType>.Instance.Insert(ct);
                return RedirectToAction("Success", "Home");
            }
            return View();
        }

        public ActionResult EditCarType(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult EditCarType(int id, FormCollection form)
        {
            return View();
        }

        public ActionResult DelCarType(string ids)
        {
            return View();
        }
        #endregion

        #region 品牌
        public ActionResult CarBrand()
        {
            List<CarBrandInfo> list = BLLFactory<CarBrand>.Instance.GetAll();
            return View(list);
        }

        public ActionResult AddCarBrand()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddCarBrand(FormCollection form)
        {
            var cb = new CarBrandInfo();
            UpdateModel(cb, new string[] { "Brand" }, form.ToValueProvider());

            if (String.IsNullOrEmpty("Brand"))
                ModelState.AddModelError("", "请填写类别名!");

            if (ModelState.IsValid)
            {
                BLLFactory<CarBrand>.Instance.Insert(cb);
                return RedirectToAction("Success", "Home");
            }

            return View();
        }

        public ActionResult EditCarBrand(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult EditCarBrand(int id, FormCollection form)
        {
            return View();
        }

        public ActionResult DelCarBrand(string ids)
        {
            return View();
        }
        #endregion

        #region 库存
        public ActionResult Inventory()
        {
            return View();
        }

        public ActionResult AddInventory()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddInventory(FormCollection form)
        {
            return View();
        }

        public ActionResult EditInventory(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult EditInventory(int id, FormCollection form)
        {
            return View();
        }

        public ActionResult DelInventory(string ids)
        {
            return View();
        }
        #endregion

    }
}
